<template>
  <div>
    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-book :to="{ path: '/home' }">首页</el-breadcrumb-book>
      <el-breadcrumb-book>{{"->"}}图书管理</el-breadcrumb-book>
      <el-breadcrumb-book>{{"->"}}图书审核</el-breadcrumb-book>
    </el-breadcrumb>

    <!-- 定义卡片 -->
    <el-card class="box-card">

      <!-- 定义第一行数据 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryBookInfo.query" clearable @clear="getBookTempList">
            <el-button slot="append" icon="el-icon-search" @click="getBookTempList"></el-button>
          </el-input>
        </el-col>

      </el-row>
      <el-table :data="bookTempList" style="width: 100%" stripe border>

        <el-table-column type="index" align="center" label="序号" width="80px">
        </el-table-column>

        <el-table-column prop="stage" align="center" label="所在级" width="80px">
        </el-table-column>

        <el-table-column prop="name" align="center" label="书名" width="100px">
        </el-table-column>

        <el-table-column prop="author" align="center" label="作者名" width="70px">
        </el-table-column>

        <!-- 使用过滤器修改时间 在main.js中定义过滤器 -->
        <el-table-column prop="price" align="center" label="价格(元)" width="80px">
          <template slot-scope="scope">
            <!-- {{scope.row.price | priceFormat}} -->
            {{scope.row.price | priceFormat}}
          </template>
        </el-table-column>

        <el-table-column prop="uploader" align="center" label="上传者" width="80px">
        </el-table-column>

        <el-table-column prop="intro" align="center" label="简介" width="140px">
        </el-table-column>

        <el-table-column prop="created" align="center" label="上传时间" width="150px">
          <template slot-scope="scope">
            <!-- {{scope.row.price | priceFormat}} -->
            {{scope.row.created | dateYMDHMSFormat}}
          </template>
        </el-table-column>

       <!-- <el-table-column prop="updated" label="更新时间" width="180px" :formatter="formatDate">
        </el-table-column> -->
        <el-table-column label="操作" align="center" width="350px">
          <template slot-scope="scope">
            <el-button type="success" icon="el-icon-notebook-2" size="mini" @click="updateBookBtn(scope.row)">详情</el-button>
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="examBookBtn(scope.row)">审核通过</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="disExamBookBtn(scope.row)">审核不通过</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 定义分页插件-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryBookInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryBookInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 定义修改页面 当对话框关闭时,将表格数据重置-->
    <el-dialog title="修改图书信息" :visible.sync="updateDialogVisible" width="50%" @close="closeUpdateDialog">
      <el-form :model="updateBookModel" ref="updateBookRef" label-width="100px">
        <el-form-book label="图书所在级" prop="stage">
          图书所在级:
          <el-input v-model="updateBookModel.stage" ></el-input>
        </el-form-book>
        <el-form-book label="书名" prop="name">
          书名:
          <el-input v-model="updateBookModel.name" ></el-input>
        </el-form-book>

        <el-form-book label="作者" prop="author">
          作者:
          <el-input v-model="updateBookModel.author" ></el-input>
        </el-form-book>
        <el-form-book label="作者简介地址" prop="author_addr">
          作者简介地址:
          <el-input v-model="updateBookModel.authorAddr" ></el-input>
        </el-form-book>

        <el-form-book label="价格" prop="price">
          价格:
          <el-input v-model="updateBookModel.price"></el-input>
        </el-form-book>

        <el-form-book label="豆瓣评分" prop="rate">
          豆瓣评分:
          <el-input v-model="updateBookModel.rate" ></el-input>
        </el-form-book>
        <el-form-book label="豆瓣评分地址" prop="rate_addr">
          豆瓣评分地址:
          <el-input v-model="updateBookModel.rateAddr" ></el-input>
        </el-form-book>

        <el-form-book label="下载地址" prop="down_addr">
          下载地址:
          <el-input v-model="updateBookModel.downAddr"></el-input>
        </el-form-book>
        <el-form-book label="下载量" prop="down_num">
          下载量:
          <el-input v-model="updateBookModel.downNum" ></el-input>
        </el-form-book>

        <el-form-book label="简介" prop="intro">
          简介:
          <el-input v-model="updateBookModel.intro"></el-input>
        </el-form-book>
        <el-form-book label="图书封面地址" prop="face_addr">
          封面地址:
          <el-input v-model="updateBookModel.faceAddr"></el-input>
        </el-form-book>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false" >取 消</el-button>
        <el-button type="primary" @click="updateBook">确 定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
  export default {
    data() {
      return {
        //1.定义商品列表信息
        bookTempList: [],
        bookTemp: {
          id: '',
          stage:'',
          name:'',
          price:'',
          uploader:'',
          intro:'',
        },
        //2.定义分页对象
        queryBookInfo: {
          query: '', //定义查询参数
          pageNum: 1,
          pageSize: 10
        },
        total: 0,
        updateDialogVisible: false,
        //定义修改用户数据封装
        updateBookModel: {}
      }
    },
    created() {
      //1.获取商品列表数据
      this.getBookTempList()
    },
    methods: {
      //实现审核书籍信息分页查询
      async getBookTempList() {
        const {
          data: result
        } = await this.$http.get("/bookTemp/getBookTempList", {
          params: this.queryBookInfo
        })
        if (result.status !== 200) return this.$message.error("书籍列表查询失败")
        this.bookTempList = result.data.rows
        this.total = result.data.total
      },

      //条数变化时 调用
      handleSizeChange(size) {
        this.queryBookInfo.pageSize = size
        this.getBookTempList()
      },
      //页码值变化时 调用
      handleCurrentChange(current) {
        this.queryBookInfo.pageNum = current
        this.getBookTempList()
      },
      //书籍通过审核
      async examBookBtn(bookTemp){
        //消息确认框
        const result = await this.$confirm('确定通过审核?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(error => error)
        if(result !== 'confirm'){
           return this.$message.info("取消审核")
        }
        const {data: result1} = await this.$http.get("/bookTemp/"+bookTemp.id)
        this.bookTemp = result1.data
        //审核表通过后,审核书籍已存到书籍数据库,所以在审核库中删除书籍
        await this.$http.delete(`/bookTemp/${result1.data.id}`)
        const {data: result2} = await this.$http.post("/bookTemp/addBookTemp", bookTemp)
        if (result2.status !== 200) return this.$message.error("书籍提交失败")
        this.$message.success("书籍提交成功")

        let msg = {
          send: window.sessionStorage.getItem('userId'),
          rece: bookTemp.uploader,
          message: "恭喜您，您上传的书籍 《"+bookTemp.name+"》 已通过审核，您可以在首页查看"
        }
        this.$http.post("/msg/sendMsg",msg)
        //重新获取书籍审核列表信息
        this.getBookTempList()
    },
    //书籍未通过审核
    async disExamBookBtn(bookTemp){
       //1.消息确认框
       const result =  await this.$confirm('确定该书不能通过审核（此操作将删除记录）?', '提示', {
                 confirmButtonText: '确定',
                 cancelButtonText: '取消',
                 type: 'warning'
               }).catch(error => error)
      if(result !== 'confirm'){
        return this.$message.info("已取消操作")
      }
      const {data: result1} = await this.$http.delete(`/bookTemp/${bookTemp.id}`)
      if(result1.status !== 200) return this.$message.error("操作失败")
      this.$message.success("操作成功")

      let msg = {
        send: window.sessionStorage.getItem('userId'),
        rece: bookTemp.uploader,
        message: "抱歉，您上传的书籍 《"+bookTemp.name+"》 未通过审核，请您仔细查看该书是否违规。"
      }
      this.$http.post("/msg/sendMsg",msg)
      //重新加载 数据
      this.getBookTempList()
    },
    //修改信息
    async updateBookBtn(book){
      this.updateDialogVisible = true
      const {data: result} = await this.$http.get("/bookTemp/"+book.id)
      if(result.status !== 200) return this.$message.error("书籍查询失败")
      this.updateBookModel = result.data
    },
    closeUpdateDialog(){
      //重置表格数据
      this.$refs.updateBookRef.resetFields()
    },
    updateBook(){
      //1.预校验数据
      this.$refs.updateBookRef.validate(async valid => {
         if(!valid)  return this.$message.error("表单验证没有通过")
         //根据接口文档要求封装数据
         let book = {}
         book.id = this.updateBookModel.id
         book.stage = this.updateBookModel.stage
         book.name = this.updateBookModel.name
         book.author = this.updateBookModel.author
         book.authorAddr = this.updateBookModel.authorAddr
         book.price = this.updateBookModel.price
         book.rate = this.updateBookModel.rate
         book.rateAddr = this.updateBookModel.rateAddr
         book.downAddr = this.updateBookModel.downAddr
         book.downNum = this.updateBookModel.downNum
         book.intro = this.updateBookModel.intro
         book.faceAddr = this.updateBookModel.faceAddr
         const {data: result} = await this.$http.put(`/bookTemp/updateBookTemp`,book)
         if(result.status !== 200) return this.$message.error("书籍修改失败")
         this.$message.success("书籍更新成功")
         this.updateDialogVisible = false
         this.getBookTempList()
      })
    },
    //利用钩子函数实现数据查询
    mounted(){
      this.getBookTempList()
    },
  },
}
</script>
<!-- 防止组件样式冲突 -->
<style lang="less" scoped>

</style>
